<?php
class Admin extends Controller
{
	function __construct()
	{	
		parent::Controller();
		$this->quickauth->restrict('admin');
	}
	
	function admin ()
	{
		self::__construct();
	}
	
	function index()
	{
		$blog_posts = $this->db->get('blog_posts');
		//$data = $data->row_array();
		$this->load->helper('date');
		$query = $this->db->get('blog_posts');
		if($query->num_rows() !== 0)
		{
			$i = 0;
			foreach ($query->result_array() as $row)
			{
				$o[$i]['id'] = $row['id'];
				$o[$i]['title'] = $row['title'];
				$o[$i]['content'] = $row['content'];
				$o[$i]['author'] = $row['author'];
				$o[$i]['timestamp'] = unix_to_human($row['timestamp']);
				$i++;
			}		
		}
		else
		{
				$o['1']['id'] = '';
				$o['1']['title'] = '';
				$o['1']['content'] = 'There are no posts in the database';
				$o['1']['author'] = '';
				$o['1']['timestamp'] = '';
		}
		
		$data['items'] = $o;		
		
		$this->load->view('header');
		$this->load->view('manage_blog', $data);
		$this->load->view('footer');			
	}
	
	function add_post ()
	{
		$this->form_validation->set_rules('title', 'Title', 'required|max_length[255]');
		$this->form_validation->set_rules('blog_content', 'Content', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('header');
			$this->load->view('add_blog_post');
			$this->load->view('footer');			
		}
		else
		{
			$post_data = array (
									'title' => $this->input->post('title'),
									'content' => $this->input->post('blog_content'),
									'author' => $_SESSION['username'],
									'timestamp' => time()
								);
			
			$this->db->insert('blog_posts', $post_data);
			
			$recent = array(
								'username' => $_SESSION['username'],
								'module' => 'blog',
								'action' => 'Added post '.$this->input->post('title')
							);
							
			$this->db->insert('recent', $recent);
			
			$data2 = array(
							'title' => 'Post Added',
							'message' => 'Your post has been added to the system.'
						);
			
			$this->load->view('header');
			$this->load->view('message', $data2);
			$this->load->view('footer');
		}
	}
	
	function edit_post ($post_id=null)
	{
		if($post_id == null){
			exit();
		}
		$this->form_validation->set_rules('title', 'Title', 'required|max_length[255]');
		$this->form_validation->set_rules('blog_content', 'Content', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->db->where('id', $post_id);
			$data = $this->db->get('blog_posts');
			$data = $data->row_array();
			
			$this->load->view('header');
			$this->load->view('edit_blog_post', $data);
			$this->load->view('footer');			
		}
		else
		{
			$post_data = array (
									'title' => $this->input->post('title'),
									'content' => $this->input->post('blog_content')
								);
			
			$this->db->where('id', $post_id);
			$this->db->update('blog_posts', $post_data);
			
			$recent = array(
								'username' => $_SESSION['username'],
								'module' => 'blog',
								'action' => 'Updated post '.$this->input->post('title')
							);
							
			$this->db->insert('recent', $recent);
			
			$data2 = array(
							'title' => 'Post Edited',
							'message' => $this->input->post('title').' has been updated.'
						);
			
			$this->load->view('header');
			$this->load->view('message', $data2);
			$this->load->view('footer');
		}
	}
	
	function delete_post ()
	{
		$post_id = $this->uri->segment(4);
		if ($post_id)
		{
			$this->db->where('id', $post_id);
			$this->db->delete('blog_posts');
			
			$recent = array(
								'username' => $_SESSION['username'],
								'module' => 'blog',
								'action' => 'Deleted post '.$post_id
							);
							
			$this->db->insert('recent', $recent);
			
			
			$data = array('body_id' => 'pages');
			
			$data2 = array(
							'title' => 'Post Deleted',
							'message' => 'The post has been deleted.'
						);
			
			$this->load->view('header', $data);
			$this->load->view('message', $data2);
			$this->load->view('footer');
		}
		else
		{
			$data2 = array(
							'title' => 'Delete Post',
							'message' => 'There was an error deleting your post.'
						);
			
			$this->load->view('header');
			$this->load->view('message', $data2);
			$this->load->view('footer');
		}
	}
}
